// Cz OS Kernel 示例
// 这是一个简化的内核示例，展示如何用 Cz 编写操作系统

const kernel = @import("../src/os/kernel.zig");
const actor_kernel = @import("../src/os/actor_kernel.zig");
const memory = @import("../src/os/memory.zig");
const hardware = @import("../src/os/hardware.zig");

// 内核 Actor 1: 定时器处理
fn timer_actor() void {
    var tick_count: u32 = 0;
    
    while (true) {
        // 接收消息
        var scheduler = actor_kernel.get_scheduler();
        const current_id = scheduler.current_actor;
        var actor = &scheduler.actors[current_id];
        
        if (actor.receive()) |msg| {
            switch (msg) {
                .timer_tick => {
                    tick_count += 1;
                    
                    // 每 100 个 tick 输出一次
                    if (tick_count % 100 == 0) {
                        hardware.VGA.write("Timer tick: ");
                        // TODO: 输出数字
                        hardware.VGA.write("\n");
                    }
                },
                else => {},
            }
        }
        
        // 让出 CPU
        scheduler.yield_current();
    }
}

// 内核 Actor 2: 键盘处理
fn keyboard_actor() void {
    while (true) {
        var scheduler = actor_kernel.get_scheduler();
        const current_id = scheduler.current_actor;
        var actor = &scheduler.actors[current_id];
        
        if (actor.receive()) |msg| {
            switch (msg) {
                .interrupt => |irq| {
                    if (irq == 1) { // 键盘中断
                        hardware.VGA.write("Key pressed!\n");
                    }
                },
                else => {},
            }
        }
        
        scheduler.yield_current();
    }
}

// 内核主函数
pub fn kernel_main() noreturn {
    // 初始化硬件
    hardware.VGA.clear();
    hardware.VGA.write("Cz Operating System\n");
    hardware.VGA.write("===================\n\n");
    
    hardware.SerialPort.init();
    hardware.SerialPort.write("Cz OS booting...\n");
    
    // 初始化内存
    var phys_alloc = memory.PhysicalAllocator.init(0x100000, 16 * 1024 * 1024);
    var actor_pools = memory.ActorMemoryPool.init(0x200000);
    
    hardware.VGA.write("Memory initialized\n");
    
    // 初始化 Actor 调度器
    actor_kernel.init_scheduler();
    var scheduler = actor_kernel.get_scheduler();
    
    // 创建内核 Actor
    _ = scheduler.spawn(timer_actor, 0x300000);
    _ = scheduler.spawn(keyboard_actor, 0x310000);
    
    hardware.VGA.write("Actors spawned\n");
    hardware.VGA.write("Kernel running...\n\n");
    
    // 主调度循环
    var tick: u32 = 0;
    while (true) {
        // 调度下一个 Actor
        if (scheduler.schedule()) |actor| {
            // 执行 Actor（简化版，实际需要上下文切换）
            actor.entry_point();
        }
        
        // 模拟定时器中断
        tick += 1;
        if (tick % 1000000 == 0) {
            // 发送定时器消息给所有 Actor
            var i: u8 = 0;
            while (i < scheduler.actor_count) : (i += 1) {
                _ = scheduler.actors[i].send(.timer_tick);
            }
        }
        
        // 空闲时休眠
        if (scheduler.actor_count == 0) {
            kernel.halt();
        }
    }
}
